<template>
  <el-dialog
    :title="dialogObj.title"
    append-to-body
    :visible.sync="dialogObj.show"
    :close-on-click-modal="false"
    width="600px"
  >
    <el-form
      ref="form"
      :model="form"
      status-icon
      label-width="120px"
      :rules="rules"
      :disabled="dialogObj.read"
      v-if="dialogObj.show"
    >
      <el-form-item label="关联活动：" prop="activityIds">
        <el-select
          v-model="form.activityIds"
          placeholder="请选择活动"
          value-key="index"
          size="mini"
          :disabled="!!dialogObj.id"
          @change="selectActivityOption"
        >
          <el-option
            v-for="(data, index) in ActivityList"
            :key="index"
            :label="data.activityName"
            :value="data.activityId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="公告信息：" prop="announcement">
        <el-input
          type="textarea"
          maxlength="500"
          show-word-limit
          :autosize="{ minRows: 9 }"
          v-model="form.announcement"
          style="width:400px;"
          placeholder="请输入公告内容"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="sub" size="mini">确 定</el-button>
      <el-button @click="dialogObj.show = false" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Cookie from "@u/cookie";
import { validateTemplateContent, validateTemplateActivity } from "@u/validate";
import { _debounce } from "@u/debounce";
import {
  addAnnouncementApi,
  updateAnnouncementApi,
  getLocalActivityApi
} from "@s/api";
export default {
  name: "dialogCom",
  props: ["dialogObj"],
  data() {
    return {
      ActivityList: [],
      form: {
        announcement: "",
        activityIds: "",
        activityNames: ""
      },
      rules: {
        announcement: [
          { required: true, message: "请输入公告信息", trigger: "blur" },
          { validator: validateTemplateContent }
        ],
        activityIds: [
          {
            required: true,
            message: "请选择活动",
            trigger: "blur"
          },
          { validator: validateTemplateActivity }
        ]
      }
    };
  },

  methods: {
    initDialog() {
      if (this.dialogObj.id) {
        this.form.announcement = this.dialogObj.form.announcement;
        this.form.activityIds = this.dialogObj.form.activityIds;
        this.form.activityNames = this.dialogObj.form.activityNames;
      } else {
        this.form.announcement = "";
        this.form.activityIds = "";
        this.form.activityNames = "";
      }
    },
    // 活动信息
    getActivityList() {
      getLocalActivityApi().then(res => {
        res = res.list;
        // 特许商品判断权限筛选
        // 查询是否有活动权限
        let getActivityId =
          JSON.parse(localStorage.getItem("mb_userInfo")).activityIds || "";
        if (getActivityId && getActivityId != "0") {
          // 单个活动用户
          getActivityId = getActivityId.split(","); // 字符串转数组
          // 活动权限筛选
          let activityArr = [];
          res.forEach(item => {
            if (getActivityId.indexOf(item.activityId) > -1) {
              activityArr.push(item);
            }
          });
          this.ActivityList = activityArr;
        } else {
          // 所有活动用户
          this.ActivityList = res;
        }
        // 判断回显的数据是否存在列表中
        if (this.dialogObj.id) {
          let findId = this.ActivityList.filter(item => {
            return item.activityId == this.form.activityIds;
          });
          if (findId.length == 0) {
            let newArr = {
              activityName: this.form.activityNames,
              activityId: this.form.activityIds
            };
            this.ActivityList.push(newArr);
          }
        }
      });
    },
    // 活动选择
    selectActivityOption(val) {
      this.form.activityIds = val;
      let name = null;
      this.ActivityList.forEach(item => {
        if (val == item.activityId) {
          name = item.activityName;
        }
      });
      this.form.activityNames = name;
    },
    sub: _debounce(function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dialogObj.id) {
            this.updateSub();
          } else {
            this.addSub();
          }
        }
      });
    }),
    addSub() {
      let json = {
        announcement: this.form.announcement,
        merchantType: 2, // 2 特许
        activityIds: this.form.activityIds,
        activityNames: this.form.activityNames
      };
      addAnnouncementApi(json).then(() => {
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "添加成功！"
        });
      });
    },
    updateSub() {
      let json = {
        id: this.dialogObj.form.id,
        announcement: this.form.announcement,
        merchantType: 2, // 2 特许
        activityIds: this.form.activityIds,
        activityNames: this.form.activityNames
      };
      updateAnnouncementApi(json).then(() => {
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "修改成功！"
        });
      });
    }
  },
  watch: {
    "dialogObj.show"(val) {
      if (val) {
        this.initDialog();
        this.getActivityList();
      }
    }
  }
};
</script>
<style scoped lang="scss"></style>
